<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\AddArtRequset;
use App\Models\Article;
use Elasticsearch\ClientBuilder;
use Illuminate\Http\Request;
use App\Exports\ArticleExport;
use Maatwebsite\Excel\Facades\Excel;
class ArticleController extends BaseController
{
    /**
     * 展示列表
     */
    public function index(Request $request)
    {
        if($request->wantsJson()){
            $where =[];
            $title = $request->input('title');
            if($title){
                $where =[['title', 'like', "%{$request->input('title')}%"]];
            }
            $data  = $this->getPage('articles',$where);
            return response()->json($data);
        }
        return view("admin.article.index");
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.article.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AddArtRequset $request)
    {
        $article = Article::create($request->only('title','desc','body','pic'));
        if($article){
           //添加文章到es
//            $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
//            $params = [
//                'index' => 'articlees',
//                'type' => '_doc',
//                'id' => $article->id,
//                'body' => [
//                    'title' => $article->title,
//                    'desn' => $article->desc,
//                ],
//            ];
//            $client->index($params);
            return $this->reSuJson('添加成功！');
        }
        return $this->reErJson('添加失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function show(Article $article)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article)
    {
        return view("admin.article.edit",compact('article'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function update(AddArtRequset $request, Article $article)
    {
        $re = $article->update($request->only('title','desc','body','pic'));
        if($re){
            return $this->reSuJson('成功');
        }
        return $this->reErJson('失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article)
    {
        $re = $article->delete();
        if($re){
            return  $this->reSuJson('成功');
        }
        return  $this->reErJson('失败');
    }

    /**
     * 导出
     */
    public function export()
    {
        return Excel::download(new ArticleExport(), 'ArticleExport.xlsx');
    }

    //生成es索引 文章信息索引
    public function esinit(){
        //得到es客户端对象
        $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
        //索引
        $params = [
            'index' => 'articlees', //索引名称
            //类型 doc
            'body' => [
                'settings' => [
                    //分区数 最大的6
                    'number_of_shards' => 5,
                    //副本数
                    'number_of_replicas' => 1
                ],
                'mappings' => [
                    '_doc' => [
                        '_source' => [
                            'enabled' => true
                        ],
                        'properties' => [
                            //设置字段 类型
                            'title' => [
                                //keyword 类型 不分词  填写什么就是什么 比如世界你好
                                'type' => 'keyword'
                            ],
                            //设置字段 类型
                            'desn' => [
                                //类型 text 可以中文分词
                                'type' => 'text',
                                //张三你好  张三  你好 张三你好
                                'analyzer' => 'ik_max_word',
                                'search_analyzer' => 'ik_max_word'
                            ]
                        ]
                    ]
                ]
            ]
        ];
        //创建索引
        $response = $client->indices()->create($params);
        dump($response);
    }
    //查询es
    public function esget(Request $request){
        //得到es客户端对象
        $client = ClientBuilder::create()->setHosts(config('es.host'))->build();
        $params = [
            'index' => 'articlees',
            'type' => '_doc',
            'body' => [
                'query' => [
                    'match' => [
                        'title'=>$request->input('title'),
                        'desn'=>$request->input('desc')
                    ]
                ]
            ]
        ];
        $results = $client->search($params);
        dump($results);
    }
}
